unit AddConfigurationGui;

interface

uses
  Results, InventionCalc, Decryptor, GlobalRecordsAndEnums, Location,

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type
  TfrmAddConfiguration = class(TForm)
    GroupBox1: TGroupBox;
    btnOk: TButton;
    btnCancel: TButton;
    lblName: TLabel;
    lblDecryptorType: TLabel;
    lblMetaLevel: TLabel;
    lblBPCMaxRuns: TLabel;
    lblBPCRuns: TLabel;
    lblBPCME: TLabel;
    lblBPCPE: TLabel;
    lblLocation: TLabel;
    edtName: TEdit;
    cbDecryptorTypes: TComboBox;
    edtMetaLevel: TEdit;
    chkBPCMaxRuns: TCheckBox;
    edtBPCME: TEdit;
    edtBPCPE: TEdit;
    cbLocation: TComboBox;
    edtBPCRuns: TEdit;
    btnHelp: TButton;
    procedure btnOkClick(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure chkBPCMaxRunsClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure btnHelpClick(Sender: TObject);
  private
    inventionCalc: TInventionCalc;
    _canclose: Boolean;
  public
    constructor Create(AOwner: TComponent; invCalc: TInventionCalc); reintroduce;
  end;

var
  frmAddConfiguration: TfrmAddConfiguration;

implementation

{$R *.dfm}

uses
  ExtActns;

constructor TfrmAddConfiguration.Create(AOwner: TComponent; invCalc: TInventionCalc);
var
  i: Integer;
  decryptortype: TDecryptorType;
  location: TLocation;
begin
  Inherited Create(AOwner);
  inventionCalc := invCalc;
  _canclose := true;

  cbDecryptorTypes.AddItem('No decryptor', nil);
  for i := 0 to inventionCalc.DecryptorTypeCount -1 do begin
    decryptortype := inventionCalc.GetDecryptorType(i);
    cbDecryptorTypes.AddItem(decryptortype.Name, decryptortype);
  end;
  cbDecryptorTypes.ItemIndex := 0; // default to no decryptor

  for i := 0 to inventionCalc.Locations.Count -1 do begin
    location := inventionCalc.Locations.GetLocation(i);
    if (location.Activity = ACT_Invention) then cbLocation.AddItem(location.Name, location);
  end;
  cbLocation.ItemIndex := cblocation.Items.Count -1;
end;

procedure TfrmAddConfiguration.btnHelpClick(Sender: TObject);
var
  run: TFileRun;
begin
  run := TFileRun.Create(self);
  run.FileName := 'help\AddConfiguration.html';
  run.Execute;
  run.Free;
end;

procedure TfrmAddConfiguration.btnOkClick(Sender: TObject);
var
  i: Integer;
  bpcruns: Integer;
begin
  _canclose := true;
  edtMetaLevel.Color := clWindow;
  edtBPCRuns.Color := clWindow;
  edtBPCME.Color := clWindow;
  edtBPCPE.Color := clWindow;
  if (not TryStrToInt(edtMetaLevel.Text, i)) then begin
    _canclose := false;
    edtMetaLevel.Color := clRed;
  end;
  if ((not chkBPCMaxRuns.Checked) and (not TryStrToInt(edtBPCRuns.Text, i))) then begin
    _canclose := false;
    edtBPCRuns.Color := clRed;
  end;
  if (not TryStrToInt(edtBPCME.Text, i)) then begin
    _canclose := false;
    edtBPCME.Color := clRed;
  end;
  if (not TryStrToInt(edtBPCPE.Text, i)) then begin
    _canclose := false;
    edtBPCPE.Color := clRed;
  end;
  if (_canclose) then begin
    if (chkBPCMaxRuns.Checked) then bpcruns := 0
    else bpcruns := StrToInt(edtBPCRuns.Text);
    inventionCalc.Results.AddConfiguration(
        edtName.Text,
        TDecryptorType(cbDecryptorTypes.Items.Objects[cbDecryptorTypes.ItemIndex]),
        StrToInt(edtMetaLevel.Text),
        chkBPCMaxRuns.Checked,
        bpcruns,
        StrToInt(edtBPCME.Text),
        StrToInt(edtBPCPE.Text),
        TLocation(cbLocation.Items.Objects[cbLocation.ItemIndex]));
  end;
end;

procedure TfrmAddConfiguration.btnCancelClick(Sender: TObject);
begin
  _canclose := true;
end;

procedure TfrmAddConfiguration.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
  CanClose := _canclose;
end;

procedure TfrmAddConfiguration.chkBPCMaxRunsClick(Sender: TObject);
begin
  edtBPCRuns.Enabled := not chkBPCMaxRuns.Checked;
  lblBPCRuns.Enabled := not chkBPCMaxRuns.Checked;
end;

end.
